package co.fitstart.admin.web.controller.courseware;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.GroupTemplateService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.entity.courseware.fitnessgroup.FitnessGroup;
import co.fitstart.entity.courseware.fitnessgroup.FitnessGroupType;

/**
 * 
 * @author Ysy
 *
 */
@Controller
@RequestMapping("/fitness-groups")
@MenuSetting("menu-course")
public class FitnessGroupController {
    
    private static final Logger logger = LoggerFactory.getLogger(FitnessGroupController.class);
    
    @Autowired
    private GroupTemplateService fitnessGroupService;

    /**
     * 
     * @param login
     * @param type
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-fitness-group")
    public ModelAndView list(@Secure Login login,
                             @RequestParam(value="type", required = false) String type,
                             Page page, HttpServletRequest request){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/fitness-group-list");
        
        if(type == null && !login.isSiteManager() && !login.isAreaManager()) {
            type = "online";
        } else if(type == null && (login.isSiteManager() || login.isAreaManager())) {
            type = "offline";
        }
        
        FitnessGroupType fitnessGroupType = FitnessGroupType.aliasOf(type);
        if(fitnessGroupType != null) {
            List<FitnessGroup> fitnessGroups = fitnessGroupService.listFitnessGroup(fitnessGroupType, page);
            
            modelAndView.addObject("fitnessGroups", fitnessGroups);
        }
        
        modelAndView.addObject("type", type);
        
        modelAndView.addObject("page", page);
        
        modelAndView.addObject("refer", "list");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @return
     */
    @RequestMapping(value = "/{fitnessGroupId}", method = RequestMethod.GET)
    @MenuSetting("submenu-fitness-group")
    public ModelAndView view(@PathVariable("fitnessGroupId") Long fitnessGroupId){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/fitness-group-view");
        
        FitnessGroup fitnessGroup = fitnessGroupService.loadFitnessGroup(fitnessGroupId);
        
        modelAndView.addObject("fitnessGroup", fitnessGroup);
        
        return modelAndView;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create(@RequestParam("type") String type){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/fitness-group-create");
        
        modelAndView.addObject("type", FitnessGroupType.aliasOf(type));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroup
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody String onCreate(FitnessGroup fitnessGroup){
        
        Result result = new Result();
        
        try{
            
            fitnessGroupService.createFitnessGroup(fitnessGroup);
            
            result.success().message("创建分组模板成功！");
        }
        catch (Exception e) {
            
            logger.error("onCreate() - ", e);
            result.fail().message("创建分组模板失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    @RequestMapping(value = "/copy", method = RequestMethod.GET)
    public ModelAndView copy(@RequestParam("type") String type){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/fitness-group-copy");
        
        modelAndView.addObject("type", FitnessGroupType.aliasOf(type));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param name
     * @param fitnessGroupId
     * @return
     */
    @RequestMapping(value = "/copy", method = RequestMethod.POST)
    public @ResponseBody String onCopy(@RequestParam("name") String name,
                                       @RequestParam("fitnessGroupId") Long fitnessGroupId){
        
        Result result = new Result();
        
        try{
            FitnessGroup fitnessGroup = fitnessGroupService.loadFitnessGroup(fitnessGroupId);
            
            fitnessGroupService.copyFitnessGroup(name, fitnessGroup);
            
            result.success().message("复制分组模板成功！");
        }
        catch (Exception e) {
            
            logger.error("onCopy() - ", e);
            result.fail().message("复制分组模板失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param fitnessGroupId
     * @return
     */
    @RequestMapping(value = "/{fitnessGroupId}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("fitnessGroupId") Long fitnessGroupId){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/fitness-group-edit");
        
        FitnessGroup fitnessGroup = fitnessGroupService.loadFitnessGroup(fitnessGroupId);
        
        modelAndView.addObject("fitnessGroup", fitnessGroup);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroup
     * @return
     */
    @RequestMapping(value = "/{fitnessGroupId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEdit(@Preloading("fitnessGroupId") FitnessGroup fitnessGroup){
        
        Result result = new Result();
        
        try{
            
            fitnessGroupService.editFitnessGroup(fitnessGroup);
            
            result.success().message("修改分组模板成功！");
        }
        catch (Exception e) {
            
            logger.error("onEdit() - ", e);
            result.fail().message("修改分组模板失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    
    /**
     * 
     * @param fitnessGroupId
     * @return
     */
    @RequestMapping(value = "/{fitnessGroupId}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("fitnessGroupId") Long fitnessGroupId){
        
        ModelAndView modelAndView = new ModelAndView("courseware/fitness-group/fitness-group-delete");
        
        modelAndView.addObject("fitnessGroupId", fitnessGroupId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param fitnessGroup
     * @return
     */
    @RequestMapping(value = "/{fitnessGroupId}", method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@Preloading("fitnessGroupId") FitnessGroup fitnessGroup){
        
        Result result = new Result();
        
        try{
            
            fitnessGroupService.deleteFitnessGroup(fitnessGroup);
            
            result.success().message("删除分组模板成功！");
        }
        catch (Exception e) {
            
            logger.error("onDelete() - ", e);
            result.fail().message("删除分组模板失败！");
        } 
        
        return JsonUtils.toJsonString(result);
    }
    
    
}
